<template>
    <div class="big_box">
        <el-breadcrumb separator="/">
            <el-breadcrumb-item>物资列表</el-breadcrumb-item>
            <el-breadcrumb-item>入库单</el-breadcrumb-item>
        </el-breadcrumb>
        <el-divider></el-divider>
        <el-form :model="selectfrom" ref="selectfrom_refs">
            <el-row :gutter="5">
                <el-col :span="3">
                    <el-input placeholder="采购单编号" v-model.trim="selectfrom.PurchaseNumber"></el-input>
                </el-col>
                <el-col :span="3">
                    <el-input placeholder="入库单编号" v-model.trim="selectfrom.InboundNumber"></el-input>
                </el-col>
                <el-col :span="3">
                    <el-input placeholder="物资名称"  v-model.trim="selectfrom.DeviceName"></el-input>
                </el-col>
                <el-col :span="3">
                    <el-input placeholder="物品编号"  v-model.trim="selectfrom.DeviceNumber"></el-input>
                </el-col>
                <el-col :span="3">
                    <el-date-picker style="width: 100%" placeholder="开始时间"
                                    type="date"
                                    value-format="yyyy-MM-dd"
                                    v-model="selectfrom.Start"></el-date-picker>
                </el-col>
                <el-col :span="3">
                    <el-date-picker style="width: 100%" placeholder="结束时间"
                                    type="date"
                                    value-format="yyyy-MM-dd"
                                    v-model="selectfrom.End"></el-date-picker>
                </el-col>
                <el-col :span="3">
                    <el-select placeholder="入库单状态"  v-model="selectfrom.State">
                        <el-option :value="0" label="全部"></el-option>
                        <el-option :value="1" label="待审批"></el-option>
                        <el-option :value="2" label="一级审批"></el-option>
                        <el-option :value="4" label="二级审批"></el-option>
                    </el-select>
                </el-col>
                <el-col :span="2" style="line-height:38px">
                    <el-switch  inactive-text="我未审核" v-model="selectfrom.NoChecked"></el-switch>
                </el-col>
                <el-col :span="1">
                    <el-button  icon="el-icon-search"  @click="formdata"></el-button>
                </el-col>
            </el-row>
        </el-form>
        <el-row style="padding: 10px 0px">
            <el-col>
                <el-button-group>
                    <el-button class="buttongroup" type="danger" icon="el-icon-delete" @click="BatchDelete_event">批量删除</el-button>
                </el-button-group>
            </el-col>
        </el-row>
        <el-row>
            <el-col :span="24">
                <el-table :data="tableData" border stripe style="width: 100%"  @selection-change="SelectionChange_event">
                    <el-table-column type="selection" width="40" align="center"></el-table-column>
                    <el-table-column fixed type="index" label="序号" width="50" align="center" :index="indexMethod"></el-table-column>
                    <el-table-column fixed prop="InboundNumber" label="编号" width="120" align="center"></el-table-column>
                    <el-table-column prop="OperatDate" label="入库日期" align="center">
                    </el-table-column>
                    <el-table-column prop="OperatId" label="经办人" width="100" align="center">
                     <template slot-scope="slot">
                            {{ GetUserName(slot.row.OperatId) }}
                        </template>
                    </el-table-column>
                    <el-table-column prop="State" label="状态" width="100" align="center">
                        <template slot-scope="slot">
                            <div v-show="slot.row.State==1" style="color: #909399">待审批</div>
                            <div v-show="slot.row.State==2" style="color: #409EFF">一级审批</div>
                            <div v-show="slot.row.State==4" style="color: #67C23A">二级审批</div>
                        </template>
                    </el-table-column>
                    <el-table-column prop="AuditUserId1" label="库管审批人" width="100" align="center">
                        <template slot-scope="slot">
                            {{ GetUserName(slot.row.AuditUserId1) }}
                        </template>
                    </el-table-column>
                    <el-table-column prop="AuditTime1" label="库管审批日期"  align="center"></el-table-column>
                    <el-table-column prop="AuditUserId2" label="财务审批人" align="center">
                        <template slot-scope="slot">
                            {{ GetUserName(slot.row.AuditUserId2) }}
                        </template>
                    </el-table-column>
                    <el-table-column prop="AuditTime2" label="财务审批日期"  align="center"></el-table-column>
                    <el-table-column fixed="right" label="操作" width="250" align="center">
                        <template slot-scope="scope">
                            <el-button type="warning" size="mini"  plain @click="InboundOrderTable_show_event(scope.row)">入库单</el-button>
                            <el-button type="info"    size="mini"  plain @click="InboundMaterialDetalis_show_event(scope.row)">查看</el-button>
                            <el-button type="danger"  size="mini"  plain @click="BatchDeleteOne_event(scope.row)">删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </el-col>
            <el-col :span="24">
                <div class="block">
                    <el-pagination
                            @size-change="formdata"
                            @current-change="formdata"
                            :current-page.sync="pagination.currentPage"
                            :page-sizes="pagination.pagesizes"
                            :page-size.sync="pagination.pagesize"
                            layout="total, sizes, prev, pager, next, jumper"
                            :total.sync="pagination.total">
                    </el-pagination>
                </div>
            </el-col>
        </el-row>

        <el-dialog :visible.sync="MaterialDetalis_dialog.dialog" :title="'入库单---'+MaterialDetalis_dialog.title"        width="90%">
            <InboundMaterialDetalis :inbound-order-detalis="InboundOrderDetalis"></InboundMaterialDetalis>
        </el-dialog>

        <el-dialog :visible.sync="InboundOrderTable_dialog.dialog" :fullscreen="true" :title="'入库单---'+InboundOrderTable_dialog.title" width="90%" :before-close="onClose">
            <InboundOrderTable :ids="Ids" :inbound-order-detalis="InboundOrderDetalis" :audit-rule="auditRule" :all-department="AllDepartment" :all-project="AllProject" :all-customer="AllCustomer"></InboundOrderTable>
        </el-dialog>
    </div>
</template>

<script>
    import InboundMaterialDetalis from "./InboundMaterialDetalis";
    import InboundOrderTable from "./InboundOrderTable";
    import * as publicjs from '/public/Scriput/Public'

    export default {
        name: "InboundOrder",
        components: { InboundOrderTable, InboundMaterialDetalis },
        props: {
            'InboundMaterialDetalis': InboundMaterialDetalis,
            'InboundOrderTable': InboundOrderTable,
        },
        data() {
            return {
                //表格数据
                tableData: [],
                //分页数据绑定
                pagination: {
                    currentPage: 1,
                    pagesizes: [10, 20, 30, 40, 50],
                    pagesize: 10,
                    total: 100
                },
                //表格查询数据
                selectfrom: {
                    InboundNumber: '',
                    PurchaseNumber:'',
                    State: 0,
                    Start: '',
                    End: '',
                    DeviceName: '',
                    DeviceNumber: '',
                    NoChecked: false,
                },
                //表格多选数据数组
                SelectMore_lis: [],
                //入库单详情页面展示
                MaterialDetalis_dialog: {
                    dialog: false,
                    title: '',
                },
                //入库单审批表格展示
                InboundOrderTable_dialog: {
                    dialog: false,
                    title: '',
                },
                //单条入库单数据详情
                InboundOrderDetalis: {},
                //入库单物资Id集合
                Ids: [],
                // OA审批规则实体
                auditRule: {},
                //所有项目
                AllProject: [],
                //所有部门
                AllDepartment: [],
                //所有供应商
                AllCustomer: [],
            }
        },
        created() {
            this.formdata()
            this.Init()
        },
        methods: {
            //初始数据加载
            Init() {
                this.AllProject = publicjs.GetAllProject(this.$http)
                this.AllDepartment = publicjs.GetAllDepartment(this.$http)
                this.AllCustomer = publicjs.GetAllCustomer(this.$http)
            },
            //表格数据序列号
            indexMethod(index) {
                return ((this.pagination.currentPage - 1) * this.pagination.pagesize) + index + 1
            },
            //初始化表格数据加载
            formdata() {
                let loadding = this.openLoading();
                this.$http.post('Device/GetInboundOrder', {
                    currentPage: this.pagination.currentPage,
                    pagesize: this.pagination.pagesize,
                    inboundOrder: this.inboundOrder,
                    from: this.selectfrom,
                }).then(res => {
                    loadding.close();
                    this.pagination.total = res.data.count
                    this.tableData = res.data.data
                }).catch(() => {
                    loadding.close();
                    this.$message.error('服务器异常,请联系管理员!')
                })
            },
            //表格单个删除
            BatchDeleteOne_event(obj) {
                this.$http.get('Device/ValidateInboundOrderState?data=' + obj.InboundNumber).then(res => {
                    if (!res.data.IsSuccess) {
                        this.$message.warning(res.data.Msg)
                        return false
                    } else {
                        this.SelectMore_lis.push(obj)
                        this.BatchDelete_event()
                    }
                })
            },
            //批量删除
            BatchDelete_event() {
                let idlis = []
                let delect_bool = true
                if (this.SelectMore_lis.length == 0) {
                    this.$message.warning('您还未选择需要删除的设备!')
                } else {
                    for (let i = 0; i < this.SelectMore_lis.length; i++) {
                        idlis.push(this.SelectMore_lis[i].InboundNumber)
                        if (this.SelectMore_lis[i].Amount > this.SelectMore_lis[i].StockAmount) {
                            this.$message.warning(this.SelectMore_lis[i].DeviceName + '存在未归还的物资，不能直接删除!')
                            delect_bool = false
                            break;
                        }
                    }
                    if (delect_bool) {
                        this.$confirm('你确定要删除吗?', '提示', {
                            confirmButtonText: '确定',
                            cancelButtonText: '取消',
                            type: 'error'
                        }).then(() => {
                            let loadding = this.openLoading();
                            this.$http.post('Device/DeleteInboundOrderContent', idlis).then(res => {
                                loadding.close();
                                if (res.data.IsSuccess) {
                                    this.$message.success(res.data.Msg)
                                    this.formdata()
                                    this.SelectMore_lis = []
                                } else {
                                    this.$message.warning(res.data.Msg)
                                    this.SelectMore_lis = []
                                }
                            }).catch(() => {
                                loadding.close();
                                this.$message.error('服务器异常,请联系管理员!')
                                this.SelectMore_lis = []
                            })
                        }).catch(() => {
                            this.$message.info('已取消!')
                            this.SelectMore_lis = []
                        })
                    }
                }
            },
            //表格数据选择事件
            SelectionChange_event(value) {
                this.SelectMore_lis = value
            },
            //获取对应人员名字
            GetUserName(value) {
                let name = ''
                if (value != 0) {
                    for (let i = 0; i < this.$store.state.AllUsers.length; i++) {
                        if (this.$store.state.AllUsers[i].Id == value) {
                            name = this.$store.state.AllUsers[i].UserName
                            return name
                        }
                    }
                }
            },
            //入库单详情页面的数据绑定
            InboundMaterialDetalis_show_event(obj) {
                this.MaterialDetalis_dialog.dialog = true
                this.MaterialDetalis_dialog.title = obj.InboundNumber
                this.$http.get('Device/ViewInboundOrder?InboundNumber=' + obj.InboundNumber).then(res => {
                    this.InboundOrderDetalis = res.data
                })
            },
            //入库单审批表格数据绑定
            InboundOrderTable_show_event(obj) {
                this.$http.get('Report/InboundOrder?InboundNumber=' + obj.InboundNumber).then(res => {
                    if (res.data.obj != null) {
                        this.Ids = []
                        res.data.obj.inboundRecord.forEach(item => {
                            this.Ids.push(item.Id)
                        })
                        this.InboundOrderDetalis = res.data.obj
                        this.auditRule = res.data.auditRule
                    }
                })
                this.InboundOrderTable_dialog.dialog = true
                this.InboundOrderTable_dialog.title = obj.InboundNumber
                let reportMianShow = window.parent.document.getElementsByClassName("reportMianShow");
                for (let i = 0; i < reportMianShow.length; i++) {
                    reportMianShow[i].style.display = "none";
                }
            },
            async onClose(done){
                await done();
                let reportMianShow = window.parent.document.getElementsByClassName("reportMianShow");
                for (let i = 0; i < reportMianShow.length; i++) {
                    reportMianShow[i].style.display = "inline-block";
                }
            },
        }
    }
</script>

<style scoped>

    .block{
        float: right;
        margin-top: 10px;
    }
    .el-divider--horizontal {
        display: block;
        height: 1px;
        width: 100%;
        margin: 20px 0;
    }
</style>
